Divisors(除數)4 <<
Previous File Overlap Solutions(文件重疊)23
Exercise 23(練習23)
Given two .txt files that have lists of numbers in them, find the numbers that are overlapping. One .txt file has a list of all prime numbers under 1000, and the other .txt file has a list of happy numbers up to 1000.
(給定.txt其中有數字列表的兩個文件,請查找重疊的數字。一個.txt文件包含所有小於1000的質數的列表,另一個.txt文件包含不超過1000的快樂數的列表。)
solution(解答)
def file_to_list(file_to_convert):
with open(file_to_convert, "r") as open_file:
list_to_return = []
line = open_file.readline()
while line:
list_to_return.append(int(line))
line = open_file.readline()
return list_to_return
def overlapping_numbers(file_one, file_two):
return set(file_to_list(file_one)) & set(file_to_list(file_two))
if __name__ == "__main__":
print(sorted(overlapping_numbers("primenumbers.txt", "happynumbers.txt")))
Divisors(除數)4 <<
Previous